/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.eproject.controler;

import aptech.eproject.DBConnection;
import java.sql.Connection;
import aptech.eproject.entity.Employees;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Loveless
 */
public class EmployeeControler {

    DBConnection db;
    Connection conn;

    public EmployeeControler() {
        db = new DBConnection();
    }

    public boolean insertEmp(Employees e) {
        try {
            Date d = new Date(System.currentTimeMillis());
            // SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

            conn = db.getConnect();
            PreparedStatement statement = conn.prepareStatement("Insert Into Employees values (?,?,?,?,?,?,?,?,?,?,?,?,?)"
                    , ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
            statement.setString(1, e.getUserName());
            statement.setString(2, e.getPassword());
            statement.setString(3, e.getEmployeeCode());
            statement.setString(4, e.getEmployeeName());
            statement.setString(5, e.getGender());
            statement.setString(6, e.getAddress());
            statement.setDate(7, e.getBirth());
            statement.setString(8, e.getPhoneHome());
            statement.setString(9, e.getMobile());
            statement.setInt(10, e.getDeptID());
            statement.setInt(11, e.getSubDeptID());
            statement.setDate(12, d);
            statement.setInt(13, e.getEStatus());
            statement.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
    
    //delete 
    
    public boolean deleteEmp(Employees e){
        try {
            PreparedStatement statement = conn.prepareStatement("Delete Employees Where EmployeeID=?"
                    , ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
            statement.setInt(1,e.getEmployeeID());
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return  false;
        }
        
    }
    
    public boolean update(Employees e){
        try {
            conn = db.getConnect();
            CallableStatement statement = conn.prepareCall("{call UpdateEmployee(?,?,?,?,?,?,?,?,?,?,?,?,?) }");
            statement.setString(1, e.getUserName());
            statement.setString(2, e.getPassword());
            statement.setString(3, e.getEmployeeCode());
            statement.setString(4, e.getEmployeeName());
            statement.setString(5, e.getGender());
            statement.setString(6, e.getAddress());
            statement.setDate(7, e.getBirth());
            statement.setString(8, e.getPhoneHome());
            statement.setString(9, e.getMobile());
            statement.setInt(10, e.getDeptID());
            statement.setInt(11, e.getSubDeptID());
            statement.setInt(12, e.getEStatus());
            statement.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
        public List<Employees> getListEmployees() throws Exception{
      
            conn = db.getConnect();
            List<Employees> lstEmp = new ArrayList<Employees>();
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("Select * from Employees");
            while (rs.next()) {
                Employees e = new Employees();
                e.setEmployeeID(rs.getInt("EmployeeID"));
                e.setUserName(rs.getString("UserName"));
                e.setPassword(rs.getString("Password"));
                e.setEmployeeCode(rs.getString("Employeecode"));
                e.setEmployeeName(rs.getString("EmployeeName"));
                e.setGender(rs.getString("Gender"));
                e.setAddress(rs.getString("Address"));
                e.setBirth(rs.getDate("Birth"));
                e.setPhoneHome(rs.getString("PhoneHome"));
                e.setMobile(rs.getString("Mobile"));
                e.setDeptID(rs.getInt("DeptID"));
                e.setSubDeptID(rs.getInt("SubDeptID"));
                e.setEDoj(rs.getDate("EDoj"));
                e.setEStatus(rs.getInt("EStatus"));
                lstEmp.add(e);
            }
            return lstEmp;
        
    }
}
